/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.medicine.service.impl;

import com.oyun.modules.medicine.domain.Doctor;
import com.oyun.modules.medicine.domain.MnPrescription;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.medicine.service.MnPrescriptionService;
import com.oyun.modules.medicine.domain.vo.MnPrescriptionQueryCriteria;
import com.oyun.modules.medicine.mapper.MnPrescriptionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;


/**
* @description 服务实现
* @author 呼和
* @date 2024-07-01
**/
@Service
@RequiredArgsConstructor
public class MnPrescriptionServiceImpl extends ServiceImpl<MnPrescriptionMapper, MnPrescription> implements MnPrescriptionService {

    private final MnPrescriptionMapper mnPrescriptionMapper;

    @Override
    public PageResult<MnPrescription> queryAll(MnPrescriptionQueryCriteria criteria, Page<Object> page){
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        return PageUtil.toPage(mnPrescriptionMapper.findAll(criteria, page));
    }

    @Override
    public List<MnPrescription> queryAll(MnPrescriptionQueryCriteria criteria){
        return mnPrescriptionMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MnPrescription resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MnPrescription resources) {
        MnPrescription mnPrescription = getById(resources.getId());
        mnPrescription.copy(resources);
        convertCode(mnPrescription);
        saveOrUpdate(mnPrescription);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MnPrescription> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MnPrescription mnPrescription : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药品用法", mnPrescription.getDrugUsage());
            map.put("药品用量", mnPrescription.getDosage());
            map.put("药品用量单位(字典)", mnPrescription.getDosageUnit());
            map.put("药剂分类(字典)", mnPrescription.getPharmaceuticalClassification());
            map.put("有效期", mnPrescription.getValidity());
            map.put("有效期单位(字典)", mnPrescription.getValidityUnit());
            map.put("药品规格", mnPrescription.getDrugSpecification());
            map.put("药品规格单位(字典)", mnPrescription.getDrugSpecificationUnit());
            map.put("药品剂量", mnPrescription.getDrugDose());
            map.put("药品剂量单位(字典)", mnPrescription.getDrugDoseUnit());
            map.put("药企", mnPrescription.getPharmaceuticalCompany());
            map.put("主治", mnPrescription.getDisease());
            map.put("药品成分", mnPrescription.getPharmaceuticalIngredient());
            map.put("蒙语药品名称", mnPrescription.getMnName());
            map.put(" mnNameCode",  mnPrescription.getMnNameCode());
            map.put(" zhName",  mnPrescription.getZhName());
            map.put("蒙语别名", mnPrescription.getMnAtherName());
            map.put(" mnAtherNameCode",  mnPrescription.getMnAtherNameCode());
            map.put(" zhAtherName",  mnPrescription.getZhAtherName());
            map.put("药品英文名称", mnPrescription.getElDrugName());
            map.put("药品分类(字典)", mnPrescription.getDrugClassification());
            map.put("蒙语药品形状与性味", mnPrescription.getMnShapeTaste());
            map.put(" mnShapeTasteCode",  mnPrescription.getMnShapeTasteCode());
            map.put(" zhShapeTaste",  mnPrescription.getZhShapeTaste());
            map.put("蒙语泡制方式", mnPrescription.getMnProcessMethod());
            map.put(" mnProcessMethodCode",  mnPrescription.getMnProcessMethodCode());
            map.put(" zhProcessMethod",  mnPrescription.getZhProcessMethod());
            map.put("蒙语功能与主治", mnPrescription.getMnIndications());
            map.put(" mnIndicationsCode",  mnPrescription.getMnIndicationsCode());
            map.put(" zhIndications",  mnPrescription.getZhIndications());
            map.put("蒙语药品注意事项", mnPrescription.getMnPrecautions());
            map.put(" mnPrecautionsCode",  mnPrescription.getMnPrecautionsCode());
            map.put(" zhPrecautions",  mnPrescription.getZhPrecautions());
            map.put("蒙语不良反应禁忌", mnPrescription.getMnContraindication());
            map.put(" mnContraindicationCode",  mnPrescription.getMnContraindicationCode());
            map.put(" zhContraindication",  mnPrescription.getZhContraindication());
            map.put("蒙语药品储藏", mnPrescription.getMnDeposit());
            map.put(" zhDeposit",  mnPrescription.getZhDeposit());
            map.put(" mnDepositCode",  mnPrescription.getMnDepositCode());
            map.put("批准文号", mnPrescription.getApprovalNumber());
            map.put("蒙语来源", mnPrescription.getMnSource());
            map.put(" mnSourceCode",  mnPrescription.getMnSourceCode());
            map.put(" zhSource",  mnPrescription.getZhSource());
            map.put("蒙语说明", mnPrescription.getMnInstructions());
            map.put(" zhInstructions",  mnPrescription.getZhInstructions());
            map.put(" createBy",  mnPrescription.getCreateBy());
            map.put(" createTime",  mnPrescription.getCreateTime());
            map.put(" updateBy",  mnPrescription.getUpdateBy());
            map.put(" updateTime",  mnPrescription.getUpdateTime());
            map.put(" status",  mnPrescription.getStatus());
            map.put(" remark",  mnPrescription.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    private void convertCode(MnPrescription mnPrescription) {
        mnPrescription.setMnNameCode(IntermediateCodeUtils.mn2Code(mnPrescription.getMnName()).replaceAll("-", " -"));
    }
}
